#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <config.h>
#define WEBSITE "http://code.google.com/p/comono/"
#define COPYRIGHT "(C) 2008 Norihiro Umeda"
#define NAME_FONT_SIZE 20
#define AUTHOR_FONT_SIZE 16
#define COPYRIGHT_FONT_SIZE 10
#define WEBBROWSER_OPEN_CMD "xdg-open "

static GHashTable *manifestTable = NULL;

const gchar *license =
    "Comono is free software; you can redistribute it and/or\n"
    "modify it under the terms of the GNU General Public License as\n"
    "published by the Free Software Foundation; either version 2 of the\n"
    "License, or (at your option) any later version.\n"
    "\n"
    "Comono is distributed in the hope that it will be useful,\n"
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
    "General Public License for more details.\n"
    "\n"
    "You should have received a copy of the GNU General Public\n"
    "License along with Comono; if not, write to the Free Software\n"
    "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA\n";

static gboolean link_clicked_cb(GtkWidget *widget, gpointer ptr)
{
    const gchar *url = gtk_link_button_get_uri(GTK_LINK_BUTTON(widget));
    GString *open_cmd = g_string_new(WEBBROWSER_OPEN_CMD);
    GError *err = NULL;
    g_string_append(open_cmd, url);
    g_spawn_command_line_async(open_cmd->str, &err);
    g_error_free(err);
    g_string_free(open_cmd, TRUE);
    return FALSE;
}

static gboolean gadget_cb(GtkMenuItem *item, gpointer ptr)
{
    GtkWindow *parent = (GtkWindow *)ptr;
    GtkWidget *dialog;
    GtkWidget *vbox;

    GtkWidget *name, *author, *desc, *copyright, *website;
    gchar *name_str, *author_str, *desc_str, *copyright_str, *website_str;

    dialog = gtk_dialog_new_with_buttons(N_("About desktop gadget"),
			    parent, 0,GTK_STOCK_OK,NULL);
    vbox = GTK_DIALOG(dialog)->vbox;
    
    if(manifestTable){
	PangoAttribute *attr;
	name_str = g_hash_table_lookup(manifestTable, "name");
	author_str = g_hash_table_lookup(manifestTable, "author");
	desc_str = g_hash_table_lookup(manifestTable, "description");
	copyright_str = g_hash_table_lookup(manifestTable, "copyright");
	website_str = g_hash_table_lookup(manifestTable, "authorWebsite");
	
	if(name_str){
	    PangoAttrList *nameAttrList = pango_attr_list_new();
	    name = gtk_label_new(name_str);
	    attr = pango_attr_size_new(NAME_FONT_SIZE*PANGO_SCALE);
	    attr->start_index = 0; attr->end_index = -1;
	    pango_attr_list_insert(nameAttrList, attr);
	    attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
	    attr->start_index = 0; attr->end_index = -1;
	    pango_attr_list_insert(nameAttrList, attr);
	    gtk_label_set_attributes(GTK_LABEL(name), nameAttrList);
	    gtk_box_pack_start_defaults(GTK_BOX(vbox),name);
	}

	if(author_str){
	    PangoAttrList *authorAttrList = pango_attr_list_new();
	    attr = pango_attr_size_new(AUTHOR_FONT_SIZE*PANGO_SCALE);
	    attr->start_index = 0; attr->end_index = -1;
	    pango_attr_list_insert(authorAttrList, attr);

	    attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
	    attr->start_index = 0; attr->end_index = -1;
	    pango_attr_list_insert(authorAttrList, attr);

	    author = gtk_label_new(author_str);
	    gtk_label_set_attributes(GTK_LABEL(author), authorAttrList);
	    gtk_box_pack_start_defaults(GTK_BOX(vbox),author);
	}

	if(copyright_str){
	    PangoAttrList *copyrightAttrList = pango_attr_list_new();
	    attr = pango_attr_size_new(COPYRIGHT_FONT_SIZE*PANGO_SCALE);
	    attr->start_index = 0; attr->end_index = -1;
	    pango_attr_list_insert(copyrightAttrList, attr);

	    copyright = gtk_label_new(copyright_str);
	    gtk_label_set_attributes(GTK_LABEL(copyright), copyrightAttrList);
	    gtk_box_pack_start_defaults(GTK_BOX(vbox),copyright);
	}

	if(desc_str){
	    GtkWidget *frame = gtk_frame_new(_("Description"));
	    desc = gtk_label_new(desc_str);
	    gtk_label_set_justify(GTK_LABEL(desc),GTK_JUSTIFY_CENTER);
	    gtk_container_add(GTK_CONTAINER(frame),desc);
	    gtk_box_pack_start_defaults(GTK_BOX(vbox),frame);
	}

	if(website_str){
	    website = gtk_link_button_new(website_str);
	    g_signal_connect(website, "clicked", G_CALLBACK(link_clicked_cb), NULL);
	    gtk_box_pack_start_defaults(GTK_BOX(vbox),website);
	}
    }
    gtk_widget_show_all(dialog);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
    return FALSE;
}

static gboolean close_cb(GtkMenuItem *item, gpointer ptr)
{
    gtk_main_quit();
    return TRUE;
}


static gboolean about_cb(GtkMenuItem *item, gpointer ptr)
{
    GtkWindow *parent = (GtkWindow *)ptr;
    g_return_val_if_fail(parent, FALSE);
    gtk_show_about_dialog (parent, 
			   "version", PACKAGE_VERSION,
			   "program-name", PACKAGE_NAME,
			   "website", WEBSITE,
			   "license", license,
			   "comments", _("A Google Desktop Gadget clone for free desktop system."),
			   "translator-credits", _("translator-credits"),
			   "copyright", COPYRIGHT,
			   NULL);
    return FALSE;
}

GtkWidget *context_menu_new(GtkWindow *parent, GHashTable *table)
{
    GtkWidget *menu = gtk_menu_new();
    GtkWidget *gadgetItem = 
	gtk_image_menu_item_new_with_label(_("About Gadget"));
    GtkWidget *aboutItem = 
	gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT, NULL);
    GtkWidget *closeItem = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLOSE, NULL);

    manifestTable = table;

    gtk_menu_shell_append(GTK_MENU_SHELL(menu), gadgetItem);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), aboutItem);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), closeItem);
    g_signal_connect(gadgetItem, "activate", G_CALLBACK(gadget_cb), parent);
    g_signal_connect(aboutItem, "activate", G_CALLBACK(about_cb), parent);
    g_signal_connect(closeItem, "activate", G_CALLBACK(close_cb), NULL);
    gtk_widget_show_all(menu);
    return menu;
}
